﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 地面类型脚本
    /// </summary>
    public class SceneGridGroundMode : ISceneGridMode
    {
        private static readonly Color32 ColorWalkable =new Color32(0, 128, 0, 96);

        private static readonly Color32 ColorBlock =new Color32(128, 0, 0, 96);

        private static readonly Color32 ColorSafty =new Color32(0, 0, 128, 96);

        private static readonly Color32 ColorObstacleWay = new Color32(192, 128, 0, 168);

        private static readonly Color32 ColorWater = new Color32(0, 0, 128, 168);

        private static readonly Color32 ColorRoadWay =new Color32(0, 128, 128, 168);

        private static readonly Color32 ColorFishing =new Color32(0, 225, 225, 180);

        private static GroundType ground = GroundType.Walkable;
        private static SceneBrushShape brushShape =SceneBrushShape.Rect;
        private static int brushsize = 3;
        private SceneGridBrushMode brush = new SceneGridBrushMode();
        public float focusX, focusY;

        public SceneGridGroundMode()
        {
            this.brush.BrushShape = brushShape;
            this.brush.BrushSize = brushsize;
        }
        public void DrawBrush(AbstractSceneGrid grid)
        {
            this.brush.DrawBrush(grid);
        }

        public void FocusPosition(AbstractSceneGrid grid, Vector3 position)
        {
            this.brush.FocusPosition(grid, position);
            this.focusX = (int)(position.x / grid.CellSize.x);
            this.focusY = (int)(position.z / grid.CellSize.y);
        }

        public Color GetCellColor(AbstractSceneCell cell)
        {
            return GetCollisionColor(cell as SceneCell);
        }

        public void IncBrushSize(int incSize)
        {
            this.brush.IncBrushSize(incSize);
        }
        
        public void OnInspectorGUI()
        {
            GUILayout.Label("Brush Shape:");
            this.brush.OnInspectorGUI();
            brushShape = this.brush.BrushShape;
            EditorGUILayout.Space();
            ground = (GroundType)EditorGUILayout.EnumPopup("Ground", ground);
        }

        public void OnSceneGUI()
        {
            if (Event.current.type == EventType.KeyDown)
            {
                if (Event.current.keyCode == (KeyCode.Alpha0))
                {
                    ground = GroundType.Block;
                }

                if (Event.current.keyCode == (KeyCode.Alpha9))
                {
                    ground = GroundType.Walkable;
                }

                if (Event.current.keyCode == (KeyCode.Alpha8))
                {
                    ground = GroundType.Safety;
                }
                if (Event.current.keyCode == (KeyCode.Alpha7))
                {
                    ground = GroundType.ObstacleWay;
                }

                if (Event.current.keyCode == (KeyCode.Alpha6))
                {
                    ground = GroundType.Road;
                }

                if (Event.current.keyCode == (KeyCode.Alpha5))
                {
                    ground = GroundType.Water;
                }
                if (Event.current.keyCode == (KeyCode.Alpha4))
                {
                    ground = GroundType.Fishing;
                }
            }
        }

        public void PaintCell(AbstractSceneGrid grid, Action<int, int> panitAction=null)
        {
            this.brush.PaintCell(grid, (x, y) => {
                var cell = grid.GetCell(x, y) as SceneCell;
                cell.Ground = ground;
                grid.SetCellColor(x, y, GetCollisionColor(cell));
            });
        }



        private static Color32 GetCollisionColor(SceneCell cell)
        {
            switch (cell.Ground)
            {
                case GroundType.Walkable:
                    return ColorWalkable;
                case GroundType.Block:
                    return ColorBlock;
                case GroundType.Safety:
                    return ColorSafty;
                case GroundType.ObstacleWay:
                    return ColorObstacleWay;
                case GroundType.Water:
                    return ColorWater;
                case GroundType.Road:
                    return ColorRoadWay;
                case GroundType.Fishing:
                    return ColorFishing;
                default:
                    return new Color32(0, 0, 0, 1);
            }
        }
    }
}

